<template>
  <div class="app-container">
    <el-card class="box-card" style="margin-bottom: 20px;border-radius: 10px;" v-show="showSearch">
    <el-form :model="queryParams" ref="queryForm" :inline="true"  label-width="68px">
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="起始时间"  prop="startTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择起始时间"
          @change="handleQuery">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="失效时间" prop="expireTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.expireTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择失效时间"
          @change="handleQuery">
        </el-date-picker>
      </el-form-item>
       <el-form-item label="状态" prop="status" >
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small" style="width: 120px" @change="handleQuery">
          <el-option v-for="dict in dict.type.license_status" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input
          v-model="queryParams.note"
          placeholder="请输入备注"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>


  <el-card class="box-card" style="margin-bottom: 20px;border-radius: 10px;">
    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleVerify"
          v-hasPermi="['edsta:license:verify']"
        >批量审核</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['edsta:license:remove']"
        >批量删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="licenseList" @selection-change="handleSelectionChange" height="800">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业名称"  prop="enterpriseName" >
        <template slot-scope="scope">
        <div align="left" v-if="scope.row.enterpriseName">{{ scope.row.enterpriseName }}</div>
        <div align="center" v-else>/</div>
      </template>
      </el-table-column>
      <el-table-column label="用户账号" align="center" prop="username" />
      <el-table-column label="联系电话" align="center" width="110" prop="phonenumber" />
      <el-table-column label="机器码" align="left" prop="machineCode" />
      <el-table-column label="功能名称" align="left"  width="220" prop="featureName" >
        <template slot-scope="scope">
          <template v-for="(item, index) in spliceFeatureName(scope.row.featureName)">
            <dict-tag :options="dict.type.feature_name" :value="item"/>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="起始时间" align="center" prop="startTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="失效时间" align="center" prop="expireTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="续费时间" align="center" prop="extendTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.extendTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" sortable align="center" prop="status" width="90" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.license_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="note" >
        <template slot-scope="scope">
        <div align="left" v-if="scope.row.note">{{ scope.row.note }}</div>
        <div align="center" v-else>/</div>
      </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="160" class-name="small-padding">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-finished"
            @click="handleVerify(scope.row,'00')"
            v-hasPermi="['edsta:license:verify']"
            v-show="scope.row.status==='02'"
          >审批</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-finished"
            @click="handleVerifyExtend(scope.row,'00')"
            v-hasPermi="['edsta:license:verify']"
            v-show="scope.row.status==='03'"
          >续费审批</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleVerify(scope.row,'01')"
            v-hasPermi="['edsta:license:verify']"
            v-show="scope.row.status==='00'"
          >撤销</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['edsta:license:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </el-card>

    <!-- 添加或修改C端用户license对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item  label="状态将改为">
            <span :style="{color: form.status==='00'?'#00afff':'#C03639'}">{{form.statusMsg}}</span>
          <!--<el-radio-group v-model="form.status">
            <el-radio :label="form.status">{{form.statusMsg}}</el-radio>
          </el-radio-group>-->
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listLicense,
    getLicense,
    delLicense,
    addLicense,
    updateLicense,
    verifyLicense,
    cancelLicense, verifyExtendLicense
  } from '@/api/system/license'
  import Template from '@/views/link/productTemplate/index'

export default {
  name: "License",
  components: { Template },
  dicts: ['license_status','feature_name'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // C端用户license表格数据
      licenseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        username:null,
        license: null,
        startTime: null,
        expireTime: null,
        status: null,
        note: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        license: [
          { required: true, message: "授权码不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "起始时间不能为空", trigger: "blur" }
        ],
        expireTime: [
          { required: true, message: "失效时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询C端用户license列表 */
    getList() {
      this.loading = true;
      listLicense(this.queryParams).then(response => {
        this.licenseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        license: null,
        startTime: null,
        expireTime: null,
        status: "0",
        note: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLicense(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改C端用户license";
      });
    },
    /** 审核按钮操作 */
    handleVerify(row,status) {
      this.reset();
      const id = row.id || this.ids
      getLicense(id).then(response => {
        this.form.id = response.data.id;
        this.form.status = status
        this.form.statusMsg = status==='00'?"通过":"撤销"
        this.open = true;
        this.title = "审批C端用户license";
      });
    },/** 续费审核按钮操作 */
    handleVerifyExtend(row,status) {
      this.reset();
      const id = row.id || this.ids
      getLicense(id).then(response => {
        this.form.id = response.data.id;
        this.form.status = status
        this.form.statusMsg = status==='00'?"通过":"撤销"
        this.form.isExtend=true;
        this.open = true;
        this.title = "审批C端用户license";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            if (this.form.status==='00'){
              if (this.form.isExtend && this.form.isExtend===true){
                verifyExtendLicense({id:this.form.id}).then(response => {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                })
              }else {
                verifyLicense({id:this.form.id}).then(response => {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                });
              }
            }else if (this.form.status === '01'){
              console.log("cancel>>>>",this.form.id)
              cancelLicense({id:this.form.id}).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            }
          } else {
            addLicense(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除C端用户license编号为"' + ids + '"的数据项？').then(function() {
        return delLicense(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('edsta/license/export', {
        ...this.queryParams
      }, `edsta_license.xlsx`)
    },
    /**
     * 切割功能名称
     */
    spliceFeatureName(featureName){
      if (featureName===undefined||featureName===null||featureName===''){
        return "";
      }
      return featureName.split(",")
    }
  }
};
</script>
